package com.example.knowledgeget1.KnowtCommon.Service;

import com.example.knowledgeget1.KnowtCommon.Entity.Study;
import com.example.knowledgeget1.KnowtCommon.Repository.StudyRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class StudyService {
    @Autowired
    private StudyRepository studyRepository;

    // 创建新学习记录
    public Study createStudy(Study study) {
        return studyRepository.save(study);
    }

    // 获取所有学习记录
    public List<Study> getAllStudies() {
        return studyRepository.findAll();
    }

    // 通过ID获取单个学习记录
    public Study getStudyById(Long id) {
        Optional<Study> study = studyRepository.findById(id);
        return study.orElse(null);
    }

    // 更新学习记录信息
    public Study updateStudy(Long id, Study studyDetails) {
        Study study = getStudyById(id);
        if (study != null) {
            study.setCourseId(studyDetails.getCourseId());
            study.setStuId(studyDetails.getStuId());
            // 更新其他字段...
            studyRepository.save(study);
        }
        return study;
    }

    // 删除学习记录
    public void deleteStudy(Long id) {
        studyRepository.deleteById(id);
    }
}